import { Component, OnInit, Injector, Input } from '@angular/core';
import { AddBase } from 'app/modules/base/add-base';
import { PersonnelService, Personnel } from '../../services/personnel.service';
import { FormBuilder, Validators } from '@angular/forms';
import { confirmPassword, markAllAsTouched } from 'app/modules/base/helper';

@Component({
  selector: 'm-reset-password',
  templateUrl: './reset-password.component.html',
  styleUrls: ['./reset-password.component.scss'],
})
export class ResetPasswordComponent extends AddBase implements OnInit {
  constructor(private injector: Injector, private personnelService: PersonnelService) {
    super(injector);
  }
  form = this.fb.group(
    {
      password: ['', Validators.required],
      confirm: [''],
    },
    {
      validators: confirmPassword(),
    },
  );
  @Input() personnel: Personnel;
  ngOnInit() { }

  async save() {
    markAllAsTouched(this.form);
    if (this.form.invalid) {
      return;
    }
    let id = this.form.value.password
    id = window['HACKER'].aes128(id)
    await this.personnelService.patch('ldap_password', id, this.personnel.id);
    this.cancel();
  }
}
